<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class MiniMe {
    
    public static $conf = array();
    
    private static $controllers = array();
    
    public static function autoload ($class) {
        require_once dirname(__FILE__) . "/" . str_replace('_', '/', strtolower($class) . '.php');
    }
    
    public static function initialize($conf = array()) {
        spl_autoload_register('MiniMe::autoload', false);
        
        // TODO add LOG
            
        // TODO add Conf
        self::initilizeConf($conf);
        
        self::initilizeControllers();
        
        date_default_timezone_set(self::$conf['date']['timezone']);
    }
    
    private static function initilizeConf(Array $conf) {
        if(isset($conf['conf_file'])) {
            $conf_file = $conf['conf_file'];
        } else {
            $conf_file = dirname(__FILE__) . '/../conf/conf.ini';
        }
        self::$conf = parse_ini_file($conf_file, true);
    }
    
    private static function initilizeControllers() {
        self::$controllers['lrdd'] = 'Controller_Xrd_Lrdd';
        self::$controllers['atom'] = 'Controller_Atom';
        self::$controllers['callback'] = 'Controller_Pubsubhubbub_Callback';
        self::$controllers['profile'] = 'Controller_Profile';
        self::$controllers['hub'] = 'Controller_Pubsubhubbub_Hub';
        self::$controllers['salmon'] = 'Controller_Salmon';
    }
    
    public static function getController($key) {
        if(isset(self::$controllers[$key])) {
            $c = self::$controllers[$key];
            //echo $c;
            return new $c($_GET, $_POST, file_get_contents('php://input'), self::$conf);
        }
        throw new Exception("Controller not found for '" . $key. "'.");
    }
}

